import {queryGoodsCategory, addGoodsCategory, delGoodsCategory, modGoodsCategory, findParentNode, findParentId} from '../services/goods';

export default {
    namespace: 'goodsCategory',

    state: {
        data: {
            list: [],
            pagination: {},
        },
        categorySList: [],
        loading: true,
    },

    effects: {
        * fetch({payload,callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryGoodsCategory, payload);
            yield put({
                type: 'save',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
            if (callback) callback(response);
        },
        * add({payload, callback}, {call, put}) {
            // yield put({
            //     type: 'changeLoading',
            //     payload: true,
            // });
            const response = yield call(addGoodsCategory, payload);
            // yield put({
            //     type: 'changeLoading',
            //     payload: false,
            // });
            //
            if (callback) callback(response);
        },
        * del({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(delGoodsCategory, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        * mod({payload, callback}, {call, put}) {
            // yield put({
            //     type: 'changeLoading',
            //     payload: true,
            // });
            const response = yield call(modGoodsCategory, payload);
            // yield put({
            //     type: 'changeLoading',
            //     payload: false,
            // });

            if (callback) callback(response);
        },
        * findParentNode({payload, callback}, {call, put}) {
            // yield put({
            //     type: 'changeLoading',
            //     payload: true,
            // });
            const response = yield call(findParentNode, payload);
            // yield put({
            //     type: 'changeLoading',
            //     payload: false,
            // });
            if (callback) callback(response);
        },
        * findParentId({payload, callback}, {call, put}) {
            const response = yield call(findParentId, payload);
            if (callback) callback(response);
        }
    },

    reducers: {
        save(state, action) {
            const categorySList = [];
            getCategorySList(categorySList, action.payload.resultData.goodsCategoryList.list);

            function getCategorySList(tmpR, list) {
                for (var i = 0; i < list.length; i++) {
                    const categoryItem = {value: list[i]["cat_id"], label: list[i]["cat_name"]};
                    if (list[i]["children"]) {
                        categoryItem["children"] = [];
                        getCategorySList(categoryItem["children"], list[i]["children"]);
                    }
                    tmpR.push(categoryItem);
                }
            }

            return {
                ...state,
                categorySList: categorySList,
                data: action.payload.resultData.goodsCategoryList,
            };
        },
        changeLoading(state, action) {
            return {
                ...state,
                loading: action.payload,
            };
        },
    },
};
